<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-delete" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList">
      <el-table-column width="55" align="center" />
      <!-- <el-table-column label="用户ID" align="center" prop="id" /> -->
      <el-table-column label="楼栋名称" align="center" prop="buildingName" />
      <el-table-column label="所属酒店" align="center" prop="hotelName" />
      <el-table-column label="房型" align="center" prop="detailName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="楼栋名称" prop="buildingName">
              <el-input v-model="form.buildingName" placeholder="请输入楼栋名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属酒店" prop="hotelId">
              <el-select v-model="form.hotelId" placeholder="请选择酒店">
                <el-option v-for="item in hotelList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="房型" prop="hotelId">
              <el-select v-model="form.detailId" placeholder="请选择房型">
                <el-option v-for="item in modelList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getBuildingList, DelBuilding, AddBuilding, UpdateBuilding, GetDetail } from "@/api/building";
import { getHotelList } from '@/api/hotel'
import {getHouseModelList} from '@/api/houseModel'
export default {
  name: "Hotel",
  data () {
    return {
      title: '',
      // 遮罩层
      loading: true,
      open: false,
      // 选中数组
      ids: [],
      form: {},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户数据
      userList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      modelList: [],
      hotelList: [],
      rules: {
        buildingName: [
          { required: true, message: "楼栋不能为空", trigger: "blur" }
        ],
        detailId: [
          { required: true, message: "请选择房型", trigger: "blur" }
        ],
        hotelId: [
          { required: true, message: "请选择酒店", trigger: "blur" }
        ]
      }
    };
  },
  created () {
    this.getList();
  },
  computed: {},
  methods: {
      /** 修改按钮操作 */
      handleUpdate(row) {
      this.reset();
      this.initSelect()
      const id = row.id || this.ids;
      GetDetail(id).then((response) => {
       this.form = response.data
        this.open = true;
        this.title = "修改套餐信息";
      });
    },
    initSelect () {
      getHotelList({
        pageNum: 1,
        pageSize: 999,
      }).then((response) => {
        this.hotelList = response.data.list.map(item => ({
          label: item.hotelName,  // 对应原始数据的 hotelName
          value: item.id          // 对应原始数据的 id
        }));
      });
      getHouseModelList({
        pageNum: 1,
        pageSize: 999,
      }).then((response) => {
        this.modelList = response.data.list.map(item => ({
          label: item.attrName,  // 对应原始数据的 hotelName
          value: item.id          // 对应原始数据的 id
        }));
      });
    },
    handleAdd (row) {
      this.reset();
      this.open = true;
      this.title = "添加楼栋";
      this.initSelect()
    },
    /** 查询用户列表 */
    getList () {
      this.loading = true;
      getBuildingList(this.queryParams).then((response) => {
        this.userList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除楼栋信息？")
        .then(function () {
          return DelBuilding(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 提交按钮 */
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            UpdateBuilding(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            AddBuilding(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        buildingName: undefined,
        hotelId: undefined,
        detailId: undefined
      };
      this.resetForm("form");
    },
  },
};
</script>
